﻿#ifndef TCPRESPONDER_H
#define TCPRESPONDER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include "myhead.h"
#include "baseresponder.h"
#include "baseresponder.h"

class TcpResponder : public BaseResponder
{
    Q_OBJECT
public:
    Q_INVOKABLE explicit TcpResponder(QObject *parent = nullptr);

signals:

public slots:

    // BaseItem interface
public:
    virtual QWidget *getConfigWidget() override;

    // BaseCommunicationZZZ interface
public:
    virtual bool connectToHost() override;
    virtual bool isConnected() override;
    virtual bool disConnect() override;

    // BaseResponder interface
public:
//    virtual void response(QString clientKey,QByteArray data) override;
    virtual void response(QByteArray data) override;

private:
    QTcpServer *_tcpServer=nullptr;
    QWaitCondition  *_waitCondition=nullptr;
    QMutex* _mutex=nullptr;
    QQueue<QByteArray> _sendQueue;

    QMap<QString, QTcpSocket*> _clients;  // 使用 QMap 存储客户端连接

    QString getKey(QTcpSocket *socket);

private slots:
    void onNewConnection();

    void onReadyRead();
};



#endif // TCPRESPONDER_H
